﻿@{
    Layout = "";

    var defaultGridPageSize = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().DefaultGridPageSize;
    var gridPageSizes = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().GridPageSizes;

}
@model TaxRateListModel
@using Nop.Web.Framework;
@using Nop.Plugin.Tax.CountryStateZip.Models;
@using Nop.Core.Infrastructure;
<table class="adminContent">
    <tr>
        <td>
            <div id="tax-countrystatezip-grid"></div>

            <script>
                $(document).ready(function () {
                    $("#tax-countrystatezip-grid").kendoGrid({
                        dataSource: {
                            type: "json",
                            transport: {
                                read: {
                                    url: "@Html.Raw(Url.Action("RatesList", "TaxCountryStateZip", new RouteValueDictionary { { "Namespaces", "Nop.Plugin.Tax.CountryStateZip.Controllers" }, { "area", "" } }))",
                                    type: "POST",
                                    dataType: "json"
                                },
                                update: {
                                    url:"@Html.Raw(Url.Action("RateUpdate", "TaxCountryStateZip", new RouteValueDictionary { { "Namespaces", "Nop.Plugin.Tax.CountryStateZip.Controllers" }, { "area", "" } }))",
                                    type: "POST",
                                    dataType: "json"
                                },
                                destroy: {
                                    url: "@Html.Raw(Url.Action("RateDelete", "TaxCountryStateZip", new RouteValueDictionary { { "Namespaces", "Nop.Plugin.Tax.CountryStateZip.Controllers" }, { "area", "" } }))",
                                    type: "POST",
                                    dataType: "json"
                                }
                            },
                            schema: {
                                data: "Data",
                                total: "Total",
                                errors: "Errors",
                                model: {
                                    id: "Id",
                                    fields: {
                                        StoreName: { editable: false, type: "string" },
                                        CountryName: { editable: false, type: "string" },
                                        StateProvinceName: { editable: false, type: "string" },
                                        Zip: { editable: true, type: "string" },
                                        TaxCategoryName: { editable: false, type: "string" },
                                        IsPrimaryDimension: { editable: false, type: "boolean" },
                                        Id: { editable: false, type: "number" }
                                    }
                                }
                            },
                            requestEnd: function (e) {
                                if (e.type == "update") {
                                    this.read();
                                }
                            },
                            error: function (e) {
                                display_kendoui_grid_error(e);
                                // Cancel the changes
                                this.cancelChanges();
                            },
                            pageSize: @(defaultGridPageSize),
                            serverPaging: true,
                            serverFiltering: true,
                            serverSorting: true
                        },
                        pageable: {
                            refresh: true,
                            pageSizes: [@(gridPageSizes)]
                        },
                        editable: {
                            confirmation: false,
                            mode: "inline"
                        },
                        scrollable: false,
                        columns: [{
                            field: "StoreName",
                            title: "@T("Plugins.Tax.CountryStateZip.Fields.Store")",
                            width: 300
                        }, {
                            field: "CountryName",
                            title: "@T("Plugins.Tax.CountryStateZip.Fields.Country")",
                            width: 200
                        }, {
                            field: "StateProvinceName",
                            title: "@T("Plugins.Tax.CountryStateZip.Fields.StateProvince")",
                            width: 200
                        }, {
                            field: "Zip",
                            title: "@T("Plugins.Tax.CountryStateZip.Fields.Zip")",
                            width: 200
                        }, {
                            field: "TaxCategoryName",
                            title: "@T("Plugins.Tax.CountryStateZip.Fields.TaxCategory")",
                            width: 200
                        }, {
                            field: "Percentage",
                            title: "@T("Plugins.Tax.CountryStateZip.Fields.Percentage")",
                            width: 100,
                            editor: function (container, options) {
                                $('<input name="' + options.field + '"/>')
                                        .appendTo(container)
                                        .kendoNumericTextBox({
                                            format: "{0:n4}",
                                            decimals: 4
                                        });
                            }
                        }, {
                            command: [{
                                name: "edit",
                                text: "@T("Admin.Common.Edit")"
                            }, {
                                name: "destroy",
                                text: "@T("Admin.Common.Delete")"
                            }],
                            width: 200
                        }]
                    });
                });
            </script>
        </td>
    </tr>
</table>
<p>
</p>
<script type="text/javascript">
    $(document).ready(function() {
        $("#@Html.FieldIdFor(model => model.AddCountryId)").change(function() {
            var selectedItem = $(this).val();
            var ddlStates = $("#@Html.FieldIdFor(model => model.AddStateProvinceId)");
            $.ajax({
                cache: false,
                type: "GET",
                url: "@(Url.Action("GetStatesByCountryId", "Country", new RouteValueDictionary { { "area", "Admin" } }))",
                data: { "countryId": selectedItem, "addAsterisk": "true" },
                success: function(data) {
                    ddlStates.html('');
                    $.each(data, function(id, option) {
                        ddlStates.append($('<option></option>').val(option.id).html(option.name));
                    });
                },
                error: function(xhr, ajaxOptions, thrownError) {
                    alert('Failed to retrieve states.');
                }
            });
        });
    });
</script>
@using (Html.BeginForm())
{
    <table class="adminContent">
        <tr>
            <td class="adminTitle" colspan="2">
                <strong>@T("Plugins.Tax.CountryStateZip.AddRecord.Hint")</strong>
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.AddStoreId):
            </td>
            <td class="adminData">
                @Html.DropDownListFor(model => model.AddStoreId, Model.AvailableStores)
                @Html.ValidationMessageFor(model => model.AddStoreId)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.AddCountryId):
            </td>
            <td class="adminData">
                @Html.DropDownListFor(model => model.AddCountryId, Model.AvailableCountries)
                @Html.ValidationMessageFor(model => model.AddCountryId)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.AddStateProvinceId):
            </td>
            <td class="adminData">
                @Html.DropDownListFor(model => model.AddStateProvinceId, Model.AvailableStates)
                @Html.ValidationMessageFor(model => model.AddStateProvinceId)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.AddZip):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.AddZip)
                @Html.ValidationMessageFor(model => model.AddZip)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.AddTaxCategoryId):
            </td>
            <td class="adminData">
                @Html.DropDownListFor(model => model.AddTaxCategoryId, Model.AvailableTaxCategories)
                @Html.ValidationMessageFor(model => model.AddTaxCategoryId)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.AddPercentage):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.AddPercentage)
                @Html.ValidationMessageFor(model => model.AddPercentage)
            </td>
        </tr>
        <tr>
            <td colspan="2" align="left">
                <input type="button" id="addtaxrate" class="k-button" value="@T("Plugins.Tax.CountryStateZip.AddRecord")" />
            </td>
        </tr>
    </table>
    <script type="text/javascript">
        $(function () {
            $('#addtaxrate').click(function () {
                $.ajax({
                    cache: false,
                    type: 'POST',
                    url: '@Url.RouteUrl("Plugin.Tax.CountryStateZip.AddTaxRate")',
                    data: $(this.form).serialize(),
                    dataType: 'json',
                    success: function (data) {
                        var grid = $("#tax-countrystatezip-grid").data('kendoGrid');
                        grid.dataSource.read();
                    },
                    error: function (xhr, ajaxOptions, thrownError) {
                        alert('Failed to add record.');
                    }
                });
                return false;
            });
        });
    </script>
}